package com.health.system.process.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class DBPoolConnection {
    static Logger log = LoggerFactory.getLogger(DBPoolConnection.class);
    private static DBPoolConnection dbPoolConnection = null;
    private static DruidDataSource druidDataSource = null;

    @PostConstruct
    private void init() throws IOException {
        Properties properties = new Properties();
        properties.load(this.getClass().getClassLoader().getResourceAsStream("db_server.properties"));
        try {
            druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            log.error("��ȡ����ʧ��");
        }
        assert (druidDataSource!= null):"DB Connection Pool Initialized Failed";
        log.info("[StatsTask] DB connection pool initialized successfully.");
    }

    /**
     * ���ݿ����ӳص���
     *
     * @return
     */
    public static DBPoolConnection getInstance() {
        if (null == dbPoolConnection) {
            synchronized (DBPoolConnection.class){
                if( dbPoolConnection == null)
                dbPoolConnection = new DBPoolConnection();
            }
        }
        return dbPoolConnection;
    }

    /**
     * ����druid���ݿ�����
     *
     * @return
     * @throws SQLException
     */
    public DruidPooledConnection getConnection() throws SQLException {
        assert druidDataSource!= null;
        return druidDataSource.getConnection();
    }

    /**
     * @param string �����ļ���
     * @return Properties����
     */
    private static Properties loadPropertiesFile(String fullFile) {
        String webRootPath = null;
        if (null == fullFile || fullFile.equals("")) {
            throw new IllegalArgumentException("Properties file path can not be null" + fullFile);
        }
        webRootPath = DBPoolConnection.class.getClassLoader().getResource("").getPath();
        webRootPath = new File(webRootPath).getParent();
        InputStream inputStream = null;
        Properties p = null;
        try {
            inputStream = new FileInputStream(new File(webRootPath + File.separator + fullFile));
            p = new Properties();
            p.load(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != inputStream) {
                    inputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return p;
    }

}